package com.mach.platform.service;

import com.google.common.collect.Maps;
import com.mach.platform.domain.base.Permission;
import com.mach.platform.repository.PermissionRepo;
import com.mach.platform.utils.DateUtil;
import com.mach.platform.utils.security.SecurityUtils;
import com.mach.platform.utils.springdata.DynamicSpecifications;
import com.mach.platform.utils.springdata.SearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @Description: 权限接口 实现类
 * @Package: com.mach.platform.com.mach.platform.service
 * @time 2018/4/23 0023 11:59
 */
@Service
public class PermissionServiceImpl implements PermissionService {

	@Autowired
	private PermissionRepo permissionRepo;

	/**
	 * 保存
	 *
	 * @param permission 保存的实体
	 * @return T 返回保存后的实体
	 */
	@Transactional(rollbackOn = Exception.class)
	@Override
	public Permission save (Permission permission) {
		permission.setCreationBy(SecurityUtils.getCurrentUserName());
		permission.setCreationTime(DateUtil.getYMDHMS());
		return permissionRepo.saveAndFlush(permission);
	}

	/**
	 * 插入，批量保存
	 *
	 * @param list 实体集合
	 * @return List<T>
	 */
	@Override
	public List<Permission> insertObjInBatch (List<Permission> list) {
		return null;
	}

	/**
	 * 更新
	 *
	 * @param permission 跟新的实体
	 * @return T
	 */
	@Transactional(rollbackOn = Exception.class)
	@Override
	public Permission update (Permission permission) {
		permission.setUpdateBy(SecurityUtils.getCurrentUserName());
		permission.setUpdateTime(DateUtil.getYMDHMS());
		return permissionRepo.saveAndFlush(permission);
	}

	/**
	 * 删除--批量删除
	 *
	 * @param ids 批量删除id
	 * @return int 影响数
	 */
	@Transactional(rollbackOn = Exception.class)
	@Override
	public int delete (Long[] ids) {

		int effections = permissionRepo.deleteByIdIn(ids);
		if (effections > 0) {
			permissionRepo.deleteRelationWhileDeletePerms(ids);
		}

		return effections;
	}

	/**
	 * 获取列表数据---使用 com.mach.platform.utils.springdata  下的DynamicSpecifications 的
	 * 写法进行分页查询
	 *
	 * @param pageable 分页参数
	 * @param params   其他参数
	 * @return Page<T>
	 */
	@Override
	public Page<Permission> list (Pageable pageable, Map params) {

		Specification condications = DynamicSpecifications
				                             .bySearchFilter(SearchFilter.parse(params).values(), Permission.class);
		return permissionRepo.findAll(condications, pageable);
	}

	/**
	 * 获取单体 数据
	 *
	 * @param id 获取详情的id
	 * @return T
	 */
	@Override
	public Permission info (Long id) {
		return permissionRepo.findById(id).orElse(null);
	}

	/**
	 * 加载角色----全部-----所关联的权限列表和【没关联的权限列表】
	 *
	 * @param roleId 要查询的角色id
	 * @return Map
	 */
	@Override
	public Map loadRolsPermissionHasOrNot (Long roleId) {

		List<Permission> hadRelatedDatas = permissionRepo.loadPermissionsInRoleRelation(roleId);
		List<Permission> hadntRelatedDatas = permissionRepo.findAll();

		Map dbResult = Maps.newHashMapWithExpectedSize(2);
		dbResult.put("inData", hadRelatedDatas);
		dbResult.put("outData", hadntRelatedDatas);

		return dbResult;
	}
}
